#ifndef _IM_CALL_CENTER_CACHE_H_
#define _IM_CALL_CENTER_CACHE_H_

#include <stdint.h>
#include <string>
#include <vector>
#include <boost/thread/shared_mutex.hpp>
#include <boost/thread/mutex.hpp>

#include "ImServiceCommon_types.h"

namespace base {
class SimpleConnectionPool;
};

namespace imserver {

class CallCenterCache {
public:
  static CallCenterCache & Instance() {
    static CallCenterCache cache;
    return cache;
  }

  int Reload();

  bool IsWaiter(int64_t qid) const {
    boost::shared_lock<boost::shared_mutex> rlock(waiter_shops_mutex_);
    std::map<int64_t, std::vector<int64_t> >::const_iterator it = waiter_shops_.find(qid);
    return it != waiter_shops_.end();
  }

  std::vector<int64_t> GetWaiterShops(int64_t waiter) const {
    {
      boost::shared_lock<boost::shared_mutex> rlock(waiter_shops_mutex_);
      std::map<int64_t, std::vector<int64_t> >::const_iterator it = waiter_shops_.find(waiter);
      if (it != waiter_shops_.end()) {
        return it->second;
      }
    }
    static std::vector<int64_t> empty;
    return empty;
  }

private:
  CallCenterCache();

  CallCenterCache(const CallCenterCache &);
  CallCenterCache& operator=(const CallCenterCache&);

  int64_t GetShopRandomWaiter(int64_t shop) const;
  int64_t GetWaiterRandomShop(int64_t waiter) const;

 private:
  base::SimpleConnectionPool * db_conn_pool_;

  // 客服可以接入的店铺列表
  std::map<int64_t, std::vector<int64_t> > waiter_shops_;
  mutable boost::shared_mutex waiter_shops_mutex_;
};

}

#endif // _IM_CALL_CENTER_CACHE_H_
